<template>
  <div class="container_detail store-edit">
    <div class="header">
      <p>门店管理>{{id?'门店编辑':'添加门店'}}</p>
      <el-button class="link" type="primary">
        <router-link :to="{path:'storeList',query: { pageIndex: currentPage }}" style="color: #fff;">返回</router-link>
      </el-button>
    </div>
    <div class="main">
      <el-form :model="user" :rules="rules" ref="user" label-width="120px" class="">
        <div class="list">
          <div class="title">{{id?'门店编辑':'添加门店'}}</div>
          <el-form-item prop="storeName">
            <div class="item">
              <div class="item-key">门店名:</div>
              <div class="item-value">
                <el-input v-model="user.storeName" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-form-item>
          <el-form-item prop="brandId">
            <div class="item">
              <div class="item-key">所属品牌:</div>
              <div class="item-value">
                <el-select v-model="user.brandId" filterable placeholder='-请选择-'>
                  <el-option v-for="(item,index) in brandArr" :key="index" :label="item.brandName" :value="item.id"></el-option>
                </el-select>
              </div>
            </div>
          </el-form-item>
          <el-form-item prop="userName">
            <div class="item">
              <div class="item-key">门店账号:</div>
              <div class="item-value">
                <el-input v-model="user.userName" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-form-item>
          <el-form-item v-if="!id" prop="pwd">
            <div class="item">
              <div class="item-key">密码:</div>
              <div class="item-value">
                  <el-input type="password" v-model="user.pwd" placeholder="请输入"></el-input>
                  <el-button type="mini" @click="setPassword()">默认密码</el-button>
              </div>
            </div>
          </el-form-item>
          <el-form-item v-if="!id" prop="pwd2">
            <div class="item">
              <div class="item-key">重复密码:</div>
              <div class="item-value">
                <el-input type="password" v-model="user.pwd2" placeholder="请输入"></el-input>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button style="margin:20px 30%;" type="primary" @click="handleSave('user')">保存</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
let CONSTANT = require("../../constant/constant.js");
let common = require("../../common.js");

export default {
  data() {
    return {
      id: "",
      info: "",
      // 返回 导航页码
      currentPage: 1,
      dialogVisible: false,
      pictureUrl: "",
      user: {
        "brandId": "",
        "brandName": "",
        "id": "",
        "pwd": "",
        "pwd2": "",
        "storeName": "",
        "userName": "",
        type:'store'
     },
      rules: {
        storeName: [{ required: true, message: "请输入门店名", trigger: "blur" }],
        brandId: [
          { required: true, message: "请选择所属品牌", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "请输入门店账号", trigger: "blur" }
        ],
        // psw: [
        //   { required: true, message: "请输入密码", trigger: "blur" }
        // ],
        // psw2: [
        //   { required: true, message: "请输入重复密码", trigger: "blur" }
        // ]
      },
      loadArr: [],
      brandArr:[]
    };
  },
  mounted() {
    this.id = this.$router.currentRoute.query.id;
    this.currentPage = this.$router.currentRoute.query.pageIndex
      ? this.$router.currentRoute.query.pageIndex
      : 1;
    if(this.id){
      this.getDetail();
    }
    this.getBrandList();
  },
  methods: {
    setPassword(){
        this.user.pwd = 123456
        this.user.pwd2 = 123456
    },
    getBrandList: function() {
      let url = CONSTANT.URL.LIST.findEsBrandByPage;
      let data = {
        pageIndex:1,
        pageSize: 1000
      };
      common.requestAjax(url, JSON.stringify(data), null, res => {
        let data = res.data;
        this.brandArr = data.bussData;
      });
    },
     getDetail() {
      let url = CONSTANT.URL.LIST.findEsStoreById;
      let data = { id: this.id }
      common.requestAjax(url, JSON.stringify(data), null, res => {
        this.user = res.data.bussData;
      });
    },
    checkImage(url) {
      this.pictureUrl = url;
      this.dialogVisible = true;
    },
    handleSave: function(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
            this.brandArr.map(item=>{
                if(item.id = this.user.brandId){
                    this.user.brandName = item.brandName
                }
            })
          let url = CONSTANT.URL.LIST.saveEsStore;
          common.requestAjax(url, JSON.stringify(this.user), null, res => {
            if (res.status == 200) {
              this.$message({
                type: "success",
                message: "操作成功!"
              });
              this.$router.push({
                path: "/storeList",
                query: { pageIndex: this.currentPage }
              });
            } else {
              this.$message({
                type: "warning",
                message: res.msg
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss">
.store-edit {
  .el-form-item {
    margin: 0px;
    
  }
  .el-form-item__error{
    top: 70%!important;
    left: 7%!important;
  }
  .item-value {
    .el-input {
      width: 100%;
    }
    .el-select {
      width: 100%;
      .el-input {
        width: 100%;
      }
    }
  }
  .el-form-item__content {
    margin: 0px !important;
  }
}
</style>

